package com.yoshino.leetcode.interview150.stack;

import java.util.Deque;
import java.util.LinkedList;

class MinStack {

    // 记录最小元素
    Deque<Integer> minStack;
    Deque<Integer> stack;

    public MinStack() {
        minStack = new LinkedList<>();
        stack = new LinkedList<>();
    }
    
    public void push(int val) {
        stack.addLast(val);
        // 当比栈顶元素小就压入
        if (minStack.isEmpty()) {
            minStack.addLast(val);
        } else if (minStack.peekLast() >= val) {
            minStack.addLast(val);
        }
    }
    
    public void pop() {
        Integer pop = stack.removeLast();
        if (minStack.peekLast().equals(pop)) {
            minStack.pollLast();
        }

    }
    
    public int top() {
        return stack.peekLast();
    }
    
    public int getMin() {
        return minStack.peekLast();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */